package com.tanhua.sso.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.tanhua.common.pojo.User;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class MyCenterService {
    @Autowired
    private UserService userService;

    @Autowired
    private SmsService smsService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public Boolean sendVerificationCode(String token) {
        //校验token
        User user = this.userService.queryUserByToken(token);
        if (ObjectUtil.isEmpty(user)) {
            return false;
        }
        ErrorResult errorResult = this.smsService.sendChecCode(user.getMobile());
        return errorResult == null;
    }

    /**
     * 校验验证码
     *
     * @param code
     * @param token
     * @return
     */
    public Boolean checkVerificationCode(String code, String token) {
        //校验token
        User user = this.userService.queryUserByToken(token);
        if (ObjectUtil.isEmpty(user)) {
            return false;
        }
        //校验验证码 先去redis中查询
        String redisKey = "CHECK_CODE_" + user.getMobile();
        String value = this.redisTemplate.opsForValue().get(redisKey);
        if (StrUtil.equals(code, value)) {
            //相等 就是验证通过
            this.redisTemplate.delete(redisKey);
            return true;
        }
        return false;
    }
    /**
     * 保存新手机号
     *
     * @return
     */
    public boolean updatePhone(String token, String newPhone) {
        //校验token
        User user = this.userService.queryUserByToken(token);
        if (ObjectUtil.isEmpty(user)) {
            return false;
        }
      Boolean result=  this.userService.updatePhone(user.getId(), newPhone);
        if(result){
            //删除以前的手机号
             String redisKey="TANHUA_USER_MOBILE_"+user.getId();//这是以前的redisKey
             this.redisTemplate.delete(redisKey);
        }
        return result;
    }
}
